<?php


    // Author: Tomasz Durka
    // Date: 4th April 2007
    // Prints out a HTML submit tag
    
    
  function text_radios ($name, array $params) {
    
    $values = false;
    $labels = false;
    $options = false;
    $class = 'text_radios';
    $checked = true;
    $id = "text_radios[$name}]";
    
    
    foreach ($params as $_name => $_value) {
      switch ($_name) {
        case 'options':
            if (is_a($_value, 'ActiveRecordCollection') || is_a($_value, 'Collection')) {
              $$_name = $_value->to_array();
            } else {
              $$_name = (array)$_value;
            }
            break;
        case 'object_value':
        case 'object_key':
        case 'first_as_false':
        case 'values':
        case 'labels':
        case 'id':
        case 'default':
          $$_name = $_value;
          break;
        case 'checked':
        case 'selected':
          $checked = $_value;
          break;
        case 'on_click':
          $pre_on_click = $_value;
        case 'class':
          $$_name = $$_name . ' ' . $_value;
          break;
      }
    }
    
    
    
    if ($options && ($values || $labels)) {
      Akrudo::raise_err("Can't specify options and (values or labels) simultaneously!");
    }
    
    if ($options) {
      $labels = array_values($options);
      $values = array_keys($options);
    } elseif ($values && $labels) {
      $labels = array_values($labels);
      $values = array_keys($values);
    } else {
      Akrudo::raise_err("You have to specify options or (values and labels)!");
    }
    
    $flat_name = preg_replace('/[\[\]]/', '_', $name);
    $r = "<span class=\"{$class}\" id=\"{$id}\">\n";
    $checked_bool = false;
    
    
    
    
    
    $on_click = "";
    $on_click .= "$(this).up().childElements().each (function (e) { e.removeClassName('checked') } );";
    $on_click .= "$(this).addClassName('checked'); $(this).getElementsBySelector('input').first().click();";
    
    
    
    $link_classes = 'link';
    if ($first_as_false) {
      if ($checked === false) {
        $checked_str = ' checked="checked"';
        $class_name = 'checked';
      } else {
        $checked_str = '';
        $class_name = '';
      }
      $r .= "  <label for=\"{$name}[false]\" class=\"{$class_name}\" onMouseOver=\"$(this).addClassName('hover');\" onMouseOut=\"$(this).removeClassName('hover');\" onMouseDown=\"{$on_click}{$pre_on_click};\">";
      $r .= "  <input style=\"display: none;\" type=\"radio\" value=\"false\" name=\"{$name}\"{$checked_str} />";
      $r .= "  <span>{$first_as_false}</span>";
      $r .= "  </label>\n";
    }
    
    
    
    for ($i = 0; $i < count($values); $i++) {
      
            if ($object_key) {
              $value = $labels[$i];
              $temp = str_replace('$', '$value->', (string)$object_key);
              $code = "\$value = \"{$temp}\";";
              eval ($code);
            } else {
              $value = $values[$i];
            }
            if ($object_value) {
              $label = $labels[$i];
              $temp = str_replace('$', '$label->', (string)$object_value);
              $code = "\$label = \"{$temp}\";";
              eval ($code);
            } else {
              $label = $labels[$i];
            }
      
      if ($checked == $value) {
        $checked_str = ' checked="checked"';
        $class_name = 'checked';
      } else {
        $checked_str = '';
        $class_name = '';
      }
      
      
      //pc ("{$name}[{$i}]");
      $r .= "  <label for=\"{$name}[{$i}]\" class=\"{$class_name}\" onMouseOver=\"$(this).addClassName('hover');\" onMouseOut=\"$(this).removeClassName('hover');\" onMouseDown=\"{$on_click}{$pre_on_click};\">";
      $r .= "  <input style=\"display: none;\" type=\"radio\" value=\"{$value}\" name=\"{$name}\" id=\"{$name}[{$i}]\"{$checked_str} />";
      $r .= "  <span>{$label}</span>";
      $r .= "  </label>\n";
    }
    
    //$r .= "  <input type=\"hidden\" id=\"{$flat_name}_radiooption\" value=\"{$value}\" name=\"{$name}\" />\n";
    $r .= "</span>\n";
    
    return $r;
  }

?>